<?php

namespace Home\Controller;

class QuestionController extends CommonController
{
    protected $page_as = 'question';

    public function _initialize()
    {
        parent::_initialize();

        // 设置user模板变量
        $uid = session('ytata.uid');
        if ($uid) {
            $user = D('User')->get_user_one($uid);
            $this->assign('user', $user);
        }
    }

    // 问题列表
    public function lists()
    {
        $cat_id = I('param.cat');
        if ($cat_id) {
            $category = D('Category')->find($cat_id);
            $title    = $category['c_title'] ?: '未知分类';
        } else {
            $title = '全部分类';
        }
        $this->assign('title', $title);
        $this->display();
    }

    // 问题详情页
    public function view()
    {
        if (session('ytata.uid')) {
            $user = D('User')->get_user_one(session('ytata.uid'), true);
            $this->assign('user', $user);
        }
        $this->display();
    }

    // 提问页
    public function add()
    {
        $SMSopen = C('YTATA_SMS_OPEN');
        $uid     = session('ytata.uid');

        if (empty($SMSopen) && empty($uid)) {
            $this->error('非法请求，短信未配置，不允许快捷发布');
        }

        $this->assign('oauth_list', YTATA_get_cache('oauth'));

        $this->display();
    }

    // 提问保存
    public function addsave()
    {
        if (IS_POST) {
            // 用户登录
            if (!D('User')->is_login()) {
                $this->check_smscode_login();  // 短信验证码快捷登录
            }

            $uid  = session('ytata.uid');
            if (empty($uid)) {
                $ret['status'] = false;
                $ret['msg'] = "请登录后操作";
                return $ret;
            }

            $ret      = D('Question')->question_add($uid, I('post.'));
            if ($ret['status'] === false) {
                $this->error($ret['msg']);
            }
            $qid = $ret['qid'];
            $this->redirect('Question/view', array('id' => $qid));
        } else {
            $this->error('非法请求');
        }
    }

    //保存回复
    public function replysave()
    {
        $uid     = session('ytata.uid');
        $qid     = I('request.qid', '0', 'intval');
        $content = I('request.content', '', 'trim');

        if (empty($uid) || empty($qid) || empty($content)) {
            $this->error("参数错误！");
        } else {
            $data['content'] = $content;
            // add answer
            $ret = D('Answer')->answer_add($uid, $qid, $data);
            if ($ret['status'] === false) {
                $this->error($ret['msg']);
            } else {
                $this->redirect('Question/view', array('id' => $qid));
            }
        }
    }

    //补充提问
    public function ajax_question_supp()
    {
        if (IS_AJAX) {
            $uid     = session('ytata.uid');
            $qid     = I('request.qid', '0', 'intval');
            $content = I('request.content', '', 'trim,htmlspecialchars');
            if (empty($uid) || empty($qid) || empty($content)) {
                $this->error("参数错误！");
            } else {
                $map['uid']     = $uid;
                $map['qid']     = $qid;
                $map['content'] = $content;
                $map['addtime'] = time();
                M('QuestionSupp')->add($map);
                $ajax['status']  = 1;
                $ajax['content'] = "提交成功！";
                $this->ajaxReturn($ajax);
            }
        }
    }

    //追问或者追答
    public function ajax_reply_asked()
    {
        if (IS_AJAX) {
            $uid             = session('ytata.uid');
            $data['rid']     = I('request.rid', '', 'intval');
            $data['type']    = I('request.type', '', 'intval');
            $data['content'] = I('request.content', '', 'trim');
            $data['addtime'] = time();
            $data['uid']     = $uid;
            if (empty($uid) || empty($data['content'])) {
                $ajax['status']  = 0;
                $ajax['content'] = "参数错误";
                $this->ajaxReturn($ajax);
            } else {
                M('AnswerReply')->add($data);
                $ajax['status']  = 1;
                $ajax['content'] = "提交成功！";
                $this->ajaxReturn($ajax);
            }
        }
    }

    //回答点赞
    public function ajax_inc_praise()
    {
        if (IS_AJAX) {
            $uid       = session('ytata.uid');
            $rid       = I('request.rid', '', 'intval');
            $qid       = I('request.qid', '', 'intval');
            $answeruid = I('request.answeruid', '', 'intval');
            if (empty($uid)) {
                $ajax['status']  = 0;
                $ajax['content'] = "请登录后操作！";
                $this->ajaxReturn($ajax);
            }
            if (empty($uid) || empty($rid)) {
                $ajax['status']  = 0;
                $ajax['content'] = "参数错误";
                $this->ajaxReturn($ajax);
            }

            $r = D('AnswerPraise')->inc_praise($rid, $uid);//增加点赞记录
            if ($r) {
                # 回答相关操作
                D('Answer')->inc_praise($rid);//修改回复点赞数字段
                $praise = D('Answer')->get_praise($rid);//获取回复点赞数量
                D('User')->inc_praise($answeruid);//更新会员点赞总数
                D('UserTrack')->add_track($uid, $qid, 4, $rid);//写入个人动态
                D('Rank')->add_record('praise', $answeruid, 1);


                $ajax['status']  = 1;
                $ajax['content'] = $praise;
                $this->ajaxReturn($ajax);
            } else {
                $ajax['status']  = 0;
                $ajax['content'] = "你已经赞过了！";
                $this->ajaxReturn($ajax);
            }
        }
    }

    //追加悬赏
    public function ajax_supp_gold()
    {
        if (IS_AJAX) {
            $uid  = session('ytata.uid');
            $qid  = I('request.qid', '', 'intval');
            $gold = I('request.gold', '', 'intval');
            if (empty($uid) || empty($qid) || empty($gold)) {
                $ajax['status']  = 0;
                $ajax['content'] = "参数错误";
                $this->ajaxReturn($ajax);
            }
            $user = D('User')->get_user_one($uid);
            if ($gold > $user['gold']) {
                $ajax['status']  = 0;
                $ajax['content'] = C('YTATA_GOLD_BYNAME') . "不够了";
                $this->ajaxReturn($ajax);
            } else {
                D('Question')->edit_reward($qid, $gold);//修改问题悬赏数
                D('User')->edit_user_gold(1, $uid, $gold, '追加悬赏扣除', false);//变更积分并且记录日志
                $ajax['status']  = 1;
                $ajax['content'] = "操作成功！";
                $this->ajaxReturn($ajax);
            }
        }
    }

    //采纳答案
    public function ajax_adoption_reply()
    {
        $uid = session('ytata.uid');
        $rid = I('request.rid', '', 'intval');
        $qid = I('request.qid', '0', 'intval');

        if (empty($uid) || empty($qid) || empty($rid)) {
            $ajax['status']  = 0;
            $ajax['content'] = "参数错误";
            $this->ajaxReturn($ajax);
        }

        $ret = D('Answer')->answer_adoption($uid, $qid, $rid);
        if ($ret['status'] === false) {
            $ajax['status']  = 0;
            $ajax['content'] = $ret['msg'];
            $this->ajaxReturn($ajax);
        } else {
            $t    = D('Task');
            $task = $t->get_task_one(12);//获取奖励规则,用于模板显示
            $this->assign('gold', $task['gold']);
            $this->assign('exp', $task['exp']);
            $answer = D('Answer')->get_answer_one_id($rid);
            $u      = D('User')->get_user_one($answer['uid']);
            $this->assign('username', $u['username']);
            $html            = $this->fetch('Ajax:ajax_adopt_answer');
            $ajax['status']  = 1;
            $ajax['content'] = $html;
            $SMSopen         = C('YTATA_SMS_OPEN');
            if ($SMSopen && !empty($u['mobile'])) {
                $params['sitename'] = C('YTATA_SITE_NAME');
                send_sms($u['mobile'], $params, 'answer_optimal');
            }
            $this->ajaxReturn($ajax);
        }
    }

    // 问题点赞
    public function ajax_question_praise()
    {
        $uid = session('ytata.uid');
        $qid = I('request.qid');
        // 用户是否登录
        if (!isset($uid)) {
            $ajax['status']  = 0;
            $ajax['content'] = "请先登录";
            $this->ajaxReturn($ajax);
        }
        if (empty($qid)) {
            $ajax['status']  = 0;
            $ajax['content'] = "请确认点赞问题";
            $this->ajaxReturn($ajax);
        }
        $ret = D('Question')->praise($uid, $qid);
        if ($ret['status'] !== 1) {
            $ajax['status']  = 0;
            $ajax['content'] = $ret['msg'];
            $this->ajaxReturn($ajax);
        }
        $ajax['status']  = 1;
        $ajax['content'] = $ret['count_praise'];
        $this->ajaxReturn($ajax);
    }
}